# 向知识库添加测试数据的PowerShell脚本

$baseUrl = "http://localhost:5239/api"

Write-Host "=== RAG AI 知识库数据初始化脚本 ===" -ForegroundColor Green
Write-Host ""

# 1. 检查测试数据状态
Write-Host "1. 检查测试数据状态..." -ForegroundColor Yellow
try {
    $statusResponse = Invoke-RestMethod -Uri "$baseUrl/seed/status" -Method GET
    Write-Host "✓ 检查完成" -ForegroundColor Green
    Write-Host "  当前是否有测试数据: $($statusResponse.hasTestData)" -ForegroundColor Cyan
    Write-Host ""
} catch {
    Write-Host "✗ 检查状态失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host ""
}

# 2. 添加测试数据
Write-Host "2. 开始添加10条测试知识库数据..." -ForegroundColor Yellow
Write-Host "   注意：此过程需要调用AI嵌入API，可能需要几分钟时间" -ForegroundColor Gray
try {
    $seedResponse = Invoke-RestMethod -Uri "$baseUrl/seed/knowledge" -Method POST
    Write-Host "✓ 数据添加成功！" -ForegroundColor Green
    Write-Host "  消息: $($seedResponse.message)" -ForegroundColor Cyan
    Write-Host "  数据条数: $($seedResponse.count)" -ForegroundColor Cyan
    Write-Host ""
} catch {
    Write-Host "✗ 添加数据失败: $($_.Exception.Message)" -ForegroundColor Red
    if ($_.Exception.Response) {
        $errorContent = $_.Exception.Response.Content.ReadAsStringAsync().Result
        Write-Host "  错误详情: $errorContent" -ForegroundColor Red
    }
    Write-Host ""
}

# 3. 再次检查状态确认
Write-Host "3. 确认数据添加结果..." -ForegroundColor Yellow
try {
    $finalStatusResponse = Invoke-RestMethod -Uri "$baseUrl/seed/status" -Method GET
    Write-Host "✓ 确认完成" -ForegroundColor Green
    Write-Host "  最终状态 - 是否有测试数据: $($finalStatusResponse.hasTestData)" -ForegroundColor Cyan
    Write-Host ""
} catch {
    Write-Host "✗ 最终确认失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host ""
}

# 4. 获取知识库列表
Write-Host "4. 获取知识库文档列表..." -ForegroundColor Yellow
try {
    $documentsResponse = Invoke-RestMethod -Uri "$baseUrl/knowledge" -Method GET
    Write-Host "✓ 获取文档列表成功" -ForegroundColor Green
    Write-Host "  文档总数: $($documentsResponse.Count)" -ForegroundColor Cyan
    
    if ($documentsResponse.Count -gt 0) {
        Write-Host "  最新文档:" -ForegroundColor Cyan
        $documentsResponse | Select-Object -First 3 | ForEach-Object {
            Write-Host "    - $($_.title) (状态: $($_.status), 块数: $($_.chunkCount))" -ForegroundColor Gray
        }
    }
    Write-Host ""
} catch {
    Write-Host "✗ 获取文档列表失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host ""
}

Write-Host "=== 数据初始化完成 ===" -ForegroundColor Green
Write-Host ""
Write-Host "现在可以使用以下测试:" -ForegroundColor Yellow
Write-Host "• 打开 RAG_API_Tests.http 文件" -ForegroundColor Cyan
Write-Host "• 运行 RAG问答测试 部分的API调用" -ForegroundColor Cyan
Write-Host "• 测试向量相似性搜索功能" -ForegroundColor Cyan
Write-Host ""
Write-Host "测试问题示例:" -ForegroundColor Yellow
Write-Host "• 什么是人工智能？" -ForegroundColor Cyan
Write-Host "• 解释机器学习的概念" -ForegroundColor Cyan
Write-Host "• 深度学习有什么特点？" -ForegroundColor Cyan
